package com.company.cloud.article.client;

import com.company.cloud.article.modules.api.response.ArticleReap;
import com.company.cloud.article.modules.cache.utils.ArticleConvertUtils;
import com.company.cloud.common.bean.BaseClientResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.article.modules.cache.models.ArticleModel;
import com.company.cloud.article.modules.api.service.ArticleClientService;
import com.company.cloud.article.modules.cache.service.ArticleCacheService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@Service
public class ArticleClientServiceImpl implements ArticleClientService {

    @Autowired
    private ArticleCacheService articleCacheService;

    @Override
    public BaseClientResult<ArticleReap> getCompanyArticleByNo(String companyArticleNo) {
        try{
            ArticleModel model = articleCacheService.getCompanyArticleByNoCache(companyArticleNo);
            return BaseClientResult.success(ArticleConvertUtils.getArticleReap(model));
        } catch (Exception e){
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<Map<String, ArticleReap>> getCompanyArticleMapByNos(Set<String> companyArticleNos) {
        try{
            Map<String, ArticleReap> data=new HashMap<>();
            if(CollectionUtils.isEmpty(companyArticleNos)){
                return BaseClientResult.success(data);
            }
            List<ArticleModel> list = articleCacheService.getCompanyArticleListByNoDB(companyArticleNos);
            if (!CollectionUtils.isEmpty(list)) {
                data = list.stream().collect(Collectors.toMap(ArticleModel::getArticleRelNo, ArticleConvertUtils::getArticleReap));
            }
            return BaseClientResult.success(data);
        } catch (Exception e){
            return BaseClientResult.error(e);
        }
    }
}
